import Vue from 'vue'
import Vuex from 'vuex'

import {
	submitInfoUrl,
	getInfoUrl,
	getArticleUrl,
	loginUrl,
	registerUrl,
	getLectureInfoUrl,
	examineFailUrl,
	examinePassUrl,
	changeMoneyUrl,
	lectureInfoDetailUrl,
	lectureUrl,
	lectureModifyUrl,
	getLectureDetailUrl,
} from '../config';
import {
	get,
	post,
	showToast
} from '@/static/utils/index';
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		loginProvider: "",
		openid: null,
		testvuex: false,
		colorIndex: 0,
		colorList: ['#FF0000', '#00FF00', '#0000FF'],
		info: {}
	},
	mutations: {
		login(state, provider) {
			state.hasLogin = true;
			state.loginProvider = provider;
		},
		logout(state) {
			state.hasLogin = false
			state.openid = null
		},
		setOpenid(state, openid) {
			state.openid = openid
		},
		setTestTrue(state) {
			state.testvuex = true
		},
		setTestFalse(state) {
			state.testvuex = false
		},
		setColorIndex(state, index) {
			state.colorIndex = index
		},
		setInfo(state, index) {
			state.info = index
		}
	},
	getters: {
		currentColor(state) {
			return state.colorList[state.colorIndex]
		}
	},
	actions: {
		//提交审核信息
		setInfo({
			state,
			commit,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(submitInfoUrl, obj)
					.then((res) => {
						if (res.code == 200) {
							commit('setInfo', res.data);
							resolve(res);
						} else {
							showToast(res.msg || res.message);
							reject(res);
						}
					});
			});
		},

		//获取审核信息
		getInfo({
			state,
			commit,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(getInfoUrl, obj)
					.then((res) => {
						if (res.code == 200) {
							// commit('getInfo', res.data);
							resolve(res);
						} else {
							if (res.code == 203)
								showToast(res.msg || res.message);
							reject(res);
						}
					});
			});
		},

		//获取合同信息
		getArticle({
			state,
			commit,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(getArticleUrl, obj)
					.then((res) => {
						if (res.code == 200) {
							// commit('getInfo', res.data);
							resolve(res);
						} else {
							// showToast(res.msg||res.message);
							reject(res);
						}
					});
			});
		},

		//登录
		login({
			state,
			commit,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(loginUrl, obj)
					.then((res) => {
						if (res.code == 200) {
							localStorage.setItem('manager_id', res.data.id);
							localStorage.setItem('is_manage', res.data.is_manage);
							resolve(res);
						} else {
							showToast(res.msg || res.message);
							reject(res);
						}
					});
			});
		},

		//注册
		register({
			state,
			commit,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(registerUrl, obj)
					.then((res) => {
						if (res.code == 200) {
							localStorage.setItem('manager_id', res.data.id);
							localStorage.setItem('is_manage', 1);
							resolve(res);
						} else {
							showToast(res.msg || res.message);
							reject(res);
						}
					});
			});
		},

		//获取讲座提交的审核信息
		getLectureInfo({
			state,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(getLectureInfoUrl, obj)
					.then((res) => {
						if (res.code == 200) {

							// commit('getInfo', res.data);
							resolve(res);
						} else {
							// showToast(res.msg||res.message);
							reject(res);
						}
					});
			});
		},

		//取消审核
		examineFail({
			state,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(examineFailUrl, obj)
					.then((res) => {
						if (res.code == 200) {

							// commit('getInfo', res.data);
							resolve(res);
						} else {
							// showToast(res.msg||res.message);
							reject(res);
						}
					});
			});
		},

		//审核通过
		examinePass({
			state,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(examinePassUrl, obj)
					.then((res) => {
						if (res.code == 200) {

							// commit('getInfo', res.data);
							resolve(res);
						} else {
							// showToast(res.msg||res.message);
							reject(res);
						}
					});
			});
		},

		//修改金额
		changeMoney({
			state,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(changeMoneyUrl, obj)
					.then((res) => {
						if (res.code == 200) {

							// commit('getInfo', res.data);
							resolve(res);
						} else {
							// showToast(res.msg||res.message);
							reject(res);
						}
					});
			});
		},

		//讲座提交的审核信息详情
		lectureInfoDetail({
			state,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(lectureInfoDetailUrl, obj)
					.then((res) => {
						if (res.code == 200) {

							// commit('getInfo', res.data);
							resolve(res);
						} else {
							// showToast(res.msg||res.message);
							reject(res);
						}
					});
			});
		},
		//获取讲座列表
		getLecture({
			state,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(lectureUrl, obj)
					.then((res) => {
						if (res.code == 200) {
							// commit('getInfo', res.data);
							resolve(res);
						} else {
							// showToast(res.msg||res.message);
							reject(res);
						}
					});
			});
		},
		//讲座详情
		getLectureDetail({
			state,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(getLectureDetailUrl, obj)
					.then((res) => {
						if (res.code == 200) {
							// commit('getInfo', res.data);
							resolve(res);
						} else {
							// showToast(res.msg||res.message);
							reject(res);
						}
					});
			});
		},
		//提交讲座
		setLecture({
			state,
			dispatch
		}, obj = {}) {
			return new Promise((resolve, reject) => {
				post(lectureModifyUrl, obj)
					.then((res) => {
						if (res.code == 200) {
							resolve(res);
						} else {
							// showToast(res.msg||res.message);
							reject(res);
						}
					});
			});
		},

	}
})

export default store
